package com.webserver.controller;

import com.webserver.core.DispatcherServlet;
import com.webserver.entity.User;
import com.webserver.http.HttpServletRequest;
import com.webserver.http.HttpServletResponse;

import java.io.*;

public class UserController {
    private static File userDir;
    private static File root;
    private static File staticDir;
    static {
        try {
            root = new File(DispatcherServlet.class.getClassLoader()
                    .getResource(".").toURI());
            staticDir = new File(root,"static");
        }catch (Exception e){
            e.printStackTrace();
        }
        userDir = new File("./users");
        if (!userDir.exists()){
            userDir.mkdirs();
        }
    }

    public void reg(HttpServletRequest request , HttpServletResponse response){
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String nickname = request.getParameter("nickname");
        String ageStr = request.getParameter("age");

        if (username==null||password==null||nickname==null
            ||ageStr==null||!ageStr.matches("[0-9]+")){
            File file = new File(staticDir,"/myweb/reg_input_error.html");
            response.setContentFile(file);
            return;
        }

        File userFile = new File(userDir,username+".obj");
        if (userFile.exists()){
            File file = new File(staticDir,"/myweb/have_user.html");
            response.setContentFile(file);
            return;
        }
        int age = Integer.parseInt(ageStr);
        User user = new User(username,password,nickname,age);
        try(
                FileOutputStream fos = new FileOutputStream(userFile);
                ObjectOutputStream oos = new ObjectOutputStream(fos)
                ){
            oos.writeObject(user);
            File file = new File(staticDir,"/myweb/reg_success.html");
            response.setContentFile(file);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void login(HttpServletRequest request , HttpServletResponse response){
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        File userFile = new File(userDir,username+".obj");
        if (!userFile.exists()){
            File file = new File(staticDir,"/myweb/login_info_error.html");
            response.setContentFile(file);
            return;
        }
        try (
                FileInputStream fis = new FileInputStream(userFile);
                ObjectInputStream ois = new ObjectInputStream(fis);
                ){
            User user = (User) ois.readObject();
            if (!user.getUsername().equals(username)||
                !user.getPassword().equals(password)){
                File file = new File(staticDir,"/myweb/login_fail.html");
                response.setContentFile(file);
                return;
            }
            File file = new File(staticDir,"/myweb/login_success.html");
            response.setContentFile(file);
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}
